import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useIntl } from '@ant-design/pro-provider';
import { parseValueToMoment } from '@ant-design/pro-utils';
import { DatePicker } from 'antd-v4';
import moment from 'moment';
import React, { useCallback } from 'react';
/**
 * 日期范围选择组件
 *
 * @param
 */

var FieldRangePicker = function FieldRangePicker(_ref, ref) {
  var text = _ref.text,
      mode = _ref.mode,
      format = _ref.format,
      render = _ref.render,
      renderFormItem = _ref.renderFormItem,
      plain = _ref.plain,
      showTime = _ref.showTime,
      fieldProps = _ref.fieldProps;
  var intl = useIntl();

  var _ref2 = Array.isArray(text) ? text : [],
      _ref3 = _slicedToArray(_ref2, 2),
      startText = _ref3[0],
      endText = _ref3[1];

  var genFormatText = useCallback(function (formatValue) {
    if (typeof (fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.format) === 'function') {
      var _fieldProps$format;

      return fieldProps === null || fieldProps === void 0 ? void 0 : (_fieldProps$format = fieldProps.format) === null || _fieldProps$format === void 0 ? void 0 : _fieldProps$format.call(fieldProps, formatValue);
    }

    return (fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.format) || format || 'YYYY-MM-DD';
  }, [fieldProps, format]); // activePickerIndex for https://github.com/ant-design/ant-design/issues/22158

  var parsedStartText = startText ? moment(startText).format(genFormatText(moment(startText))) : '';
  var parsedEndText = endText ? moment(endText).format(genFormatText(moment(endText))) : '';

  if (mode === 'read') {
    var dom = _jsxs("div", {
      ref: ref,
      children: [_jsx("div", {
        children: parsedStartText || '-'
      }), _jsx("div", {
        children: parsedEndText || '-'
      })]
    });

    if (render) {
      return render(text, _objectSpread({
        mode: mode
      }, fieldProps), _jsx("span", {
        children: dom
      }));
    }

    return dom;
  }

  if (mode === 'edit' || mode === 'update') {
    var momentValue = parseValueToMoment(fieldProps.value);

    var _dom = _jsx(DatePicker.RangePicker, _objectSpread(_objectSpread({
      ref: ref,
      format: format,
      showTime: showTime,
      placeholder: [intl.getMessage('tableForm.selectPlaceholder', '请选择'), intl.getMessage('tableForm.selectPlaceholder', '请选择')],
      bordered: plain === undefined ? true : !plain
    }, fieldProps), {}, {
      value: momentValue
    }));

    if (renderFormItem) {
      return renderFormItem(text, _objectSpread({
        mode: mode
      }, fieldProps), _dom);
    }

    return _dom;
  }

  return null;
};

export default /*#__PURE__*/React.forwardRef(FieldRangePicker);